package com.example.tellwin.mine.presenter;

import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.mine.bean.AttentionTeacherContentBean;
import com.example.tellwin.mine.contract.AttentionTeacherContract;
import com.example.tellwin.utils.HttpUtils;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/12/4
 * @Describe ：
 **/
public class AttentionTeacherPresenter extends RxPresenter<AttentionTeacherContract.View> implements AttentionTeacherContract.Presenter<AttentionTeacherContract.View> {

    private TwjfApi mApi;

    private final int pageSize = 10;
    private int questionPages;
    private int questionPageNum;
    private boolean isNotMoreQuestion;
    private int teacherPages;
    private int teacherPageNum;
    private boolean isNotMoreTeacher;

    @Inject
    public AttentionTeacherPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }


    @Override
    public void getAttentionTeacherList(boolean isLoadMore) {
        if(isLoadMore){
            if (isNotMoreTeacher) {
                return;
            }
            if (teacherPages != 0 && teacherPageNum == teacherPages) {
                isNotMoreTeacher = true;
                return;
            }
            teacherPageNum++;
        }else {
            teacherPageNum = 1;
            teacherPages = 0;
            isNotMoreTeacher = false;
        }

        mApi.getAttentionTeacherList("2" , teacherPageNum+"",pageSize+"", new SimpleCallback<AttentionTeacherContentBean>(mView) {
            @Override
            public void onSuccess(AttentionTeacherContentBean attentionTeacherContentBean) {
                teacherPageNum = attentionTeacherContentBean.getPageNum();
                teacherPages = attentionTeacherContentBean.getPages();
                if (attentionTeacherContentBean.getMyFollowList() == null || attentionTeacherContentBean.getMyFollowList().size() == 0) {
                    isNotMoreTeacher = true;
                    return;
                }
                mView.attentionTeacherListResult(isLoadMore , attentionTeacherContentBean.getMyFollowList());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void attention(int position, String teacherId) {
        RequestBody requestBody = HttpUtils.getRequestBody("followType", "1" + "", "relationId", teacherId, "relationType", "1");
        mApi.follow(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.attention(position ,teacherId);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
